package com.cungmua.app.activity;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cungmua.app.R;
import com.cungmua.app.adapter.ListDealAdapter;
import com.cungmua.app.base.BaseDrawerActivitys;
import com.cungmua.app.model.Cart;
import com.cungmua.app.model.Deal;
import com.cungmua.app.model.response.DealResponse;
import com.cungmua.app.model.response.Pager;
import com.cungmua.app.view.EndlessScrollListener;

import java.util.ArrayList;
import java.util.List;

import eu.erikw.PullToRefreshListView;

/**
 * Created by kelvin.nguyen on 6/17/14.
 */
public abstract class DealListActivity extends BaseDrawerActivitys{
    private  PullToRefreshListView listDeal;
    private ListDealAdapter adapter;
    private ImageButton btn_hot,btn_new;
    private TextView txt_deal_title;
    private ImageView icon_home;
    private Button icon_cart;
    private LinearLayout btnCart;


    @Override
    public int getLayoutResource() {
        return R.layout.activity_deal_list;
    }

    class EndlessDealListener extends EndlessScrollListener {
        @Override
        public void onLoadMore(int page, int totalItemsCount) {
            if (pager == null || totalItemsCount >= pager.getTotalRow()) {
                return;
            }
            currentPage = page;
            updateView();
        }
    };
    private EndlessScrollListener endlessScrollListener = new EndlessDealListener();
    private Pager pager;
    protected int currentPage = 1;

    @Override
    public void loadControls(Bundle savedInstanceState) {
        LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View view = inflater.inflate(R.layout.header_list_deals,null);
        LinearLayout header_root=(LinearLayout)view.findViewById(R.id.header_root);

        btn_hot=(ImageButton)view.findViewById(R.id.btn_hotest);
        btn_new=(ImageButton)view.findViewById(R.id.btn_new);

        txt_deal_title=(TextView)view.findViewById(R.id.txt_deal_title);
        btn_hot.setImageResource(R.drawable.btn_hot_active);
        btn_hot.setOnClickListener(onClickListener);
        btn_new.setOnClickListener(onClickListener);

        LayoutInflater inflater_header = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View header_view = inflater_header.inflate(R.layout.header,null);

        icon_home=(ImageView)findViewById(R.id.icon_home);
        icon_cart=(Button)findViewById(R.id.icon_cart);
        btnCart=(LinearLayout)findViewById(R.id.btnCart);

//        if(getIntent().getExtras()!=null){
//            dealType= getIntent().getExtras().getInt("type");
//        }
//        setDealType(dealType);

        Cart currentCart=getCurrentCart();
        if(currentCart.getTotalQuantity()>0) {
            icon_cart.setText(currentCart.getTotalQuantity() + "");
            icon_cart.setVisibility(View.VISIBLE);
        }else{
            icon_cart.setVisibility(View.GONE);
        }



        icon_home.setOnClickListener(new ImageView.OnClickListener() {
            @Override
            public void onClick(View v) {
              showMenu();
            }
        });

        btnCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goToYourCart();
            }
        });


        listDeal = (PullToRefreshListView) findViewById(R.id.gridDeal);
        adapter = new ListDealAdapter(this);
        pager = new Pager();
        listDeal.setAdapter(adapter);
        listDeal.setDivider(null);
        listDeal.setDividerHeight(0);
        listDeal.addHeaderView(header_root);
//        listDeal.setOnItemClickListener(onItemClickListener);
        listDeal.setOnRefreshListener(new PullToRefreshListView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                currentPage = 1;
                updateView();
            }
        });
        listDeal.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Deal deal = adapter.getItem(i);
                goToDealDetail(deal.getId());
            }
        });
    }



    public void onListDealData(DealResponse response) {
        if (response.getCode() == 0) {
            listDeal.onRefreshComplete();
//            listDeal.smoothScrollToPosition(0);
            if (currentPage == 1) {
                Log.d("Kelvin==========",response.toString());

                List<Deal> data = response.getData();
                if (data == null || data.size() < 1) {
                    data = new ArrayList<Deal>();
                }
                if (pager == null)
                    pager = new Pager();

                adapter.setDataSource(data);
                pager = response.getPager();
                endlessScrollListener = new EndlessDealListener();
                listDeal.setOnScrollListener(endlessScrollListener);
            } else {
                adapter.addToDataSource(response.getData());
            }
        }
    }

    public View.OnClickListener onClickListener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.btn_hotest:
                    setDealType(0);
                    currentPage=1;
                    updateView();
                    setBackgroudImageButton(true);
                    break;
                case R.id.btn_new:
                    setDealType(1);
                    currentPage=1;
                    updateView();
                    setBackgroudImageButton(false);
                    break;
                case R.id.icon_home:
                    break;
                case R.id.icon_cart:
                    Toast.makeText(DealListActivity.this,"kakakka",Toast.LENGTH_LONG).show();
                    break;
                default:

                    break;
            }
        }
    };
    public void setBackgroudImageButton(boolean isHot){
        if(isHot) {
            btn_hot.setImageResource(R.drawable.btn_hot_active);
            btn_new.setImageResource(R.drawable.btn_new);
        }else{
            btn_hot.setImageResource(R.drawable.btn_hot);
            btn_new.setImageResource(R.drawable.btn_new_active);
        }
    }

    public void setTitleDeal(String title){
        txt_deal_title.setText(title);
    }
    public void setDealType(int type){};

    public void setEmptyView(String msg) {
        TextView empty = (TextView) findViewById(R.id.lbl_empty);
        empty.setText(msg);
        listDeal.setEmptyView(empty);
    }

}
